package com.wwq.eleme.view.widget.indicator;

import android.content.Context;
import android.content.res.Resources;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wwq.eleme.view.widget.indicator.utils.ColorGradient;


public class OnTransitionTextListener implements Indicator.OnTransitionListener {
    private float selectSize = -1;
    private float unSelectSize = -1;
    private ColorGradient gradient;
    private float dFontFize = -1;

    private boolean isPxSize = false;

    public OnTransitionTextListener() {
        super();
    }

    public OnTransitionTextListener(float selectSize, float unSelectSize, int selectColor, int
            unSelectColor) {
        super();
        setColor(selectColor, unSelectColor);
        setSize(selectSize, unSelectSize);
    }

    public final OnTransitionTextListener setSize(float selectSize, float unSelectSize) {
        isPxSize = false;
        this.selectSize = selectSize;
        this.unSelectSize = unSelectSize;
        this.dFontFize = selectSize - unSelectSize;
        return this;
    }

    public final OnTransitionTextListener setValueFromRes(Context context, int selectColorId, int
            unSelectColorId, int selectSizeId,
                                                          int unSelectSizeId) {
        setColorId(context, selectColorId, unSelectColorId);
        setSizeId(context, selectSizeId, unSelectSizeId);
        return this;
    }

    public final OnTransitionTextListener setColorId(Context context, int selectColorId, int
            unSelectColorId) {
        setColor(ContextCompat.getColor(context, selectColorId), ContextCompat.getColor(context,
                unSelectColorId));
        return this;
    }

    public final OnTransitionTextListener setSizeId(Context context, int selectSizeId, int
            unSelectSizeId) {
        Resources res = context.getResources();
        setSize(res.getDimensionPixelSize(selectSizeId), res.getDimensionPixelSize(unSelectSizeId));
        isPxSize = true;
        return this;
    }

    public final OnTransitionTextListener setColor(int selectColor, int unSelectColor) {
        gradient = new ColorGradient(unSelectColor, selectColor, 100);
        return this;
    }

    /**
     * 如果tabItemView 不是目标的TextView，那么你可以重写该方法返回实际要变化的TextView
     *
     * @param tabItemView Indicator的每一项的view
     * @param position    view在Indicator的位置索引
     * @return
     */
    private TextView getTextView(ViewGroup tabItemView, int position) {
        return (TextView) tabItemView.getChildAt(0);
    }

    @Override
    public void onTransition(View view, int position, float selectPercent) {
        TextView selectTextView;
        if (view instanceof TextView) {
            selectTextView = (TextView) view;
        } else {
            selectTextView = getTextView((ViewGroup) view, position);
        }
        if (gradient != null) {
            selectTextView.setTextColor(gradient.getColor((int) (selectPercent * 100)));
        }
        if (unSelectSize > 0 && selectSize > 0) {
            if (isPxSize) {
                selectTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, unSelectSize + dFontFize *
                        selectPercent);
            } else {
                selectTextView.setTextSize(unSelectSize + dFontFize * selectPercent);
            }

        }
    }

}
